<script setup lang="ts">
import { gitInit } from './git_init'
import { watchEffect } from 'vue'
import { fileDirSelect } from './file_dir_select'
import SetRepository from './SetRepository.vue'
import { openInitRepository } from './openInitRepository'

const { browseFolder, selectedPath } = fileDirSelect()

const { show, initVisible } = openInitRepository()
const { getFolderInfo, initDir, watchAndSync } = gitInit()
watchEffect(() => {
  if (selectedPath.value) {
    getFolderInfo(selectedPath.value).then((res) => {
      if (res) {
        console.log(res)
        watchAndSync(selectedPath.value)
      } else {
        show()
      }
    })
  }
})

function handleSubmit(form) {
  form.url = 'https://gitee.com/s_md/test_vue.git'
  initDir({ ...form, baseDir: selectedPath.value }).then(() => {
    console.log('初始化成功')
    selectedPath.value = null
  })
}
</script>

<template>
  <div>{{ selectedPath }}</div>
  <el-button @click="browseFolder">选择要同步文件夹</el-button>
  <set-repository v-model="initVisible" @submit="handleSubmit" />
</template>
